package api

import (
	"api/basic/config"
	"api/basic/pkg/jwt"
	"api/basic/proto/user"
	"api/handler/request"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

const JWTAME = "2210A"

func UserLogin(c *gin.Context) {
	var data request.UserLoginReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": err.Error(),
		})
		return
	}
	// Contact the server and print out its response.

	r, err := config.Us.UserLogin(c, &user.UserLoginReq{
		Name:     data.Name,
		Mobile:   data.Mobile,
		Password: data.Password,
		Code:     int32(data.Code),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	claims := jwt.CustomClaims{ID: uint(r.UserId)}
	token, err := jwt.NewJWT(JWTAME).CreateToken(claims)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"msg":  "登录",
		"data": token,
	})
	log.Printf("Greeting: %s", r)

}

func UserSuccess(c *gin.Context) {
	var data request.UserSuccessReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": err.Error(),
		})
		return
	}
	r, err := config.Us.UserSuccess(c, &user.UserSuccessReq{
		Mobile: data.Mobile,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"msg":  "发送成功",
		"data": r,
	})
	log.Printf("Greeting: %s", r)
}
